package com.ruoyi.web.controller.basice_info;

import com.ruoyi.basice_info.domain.TbAssessmentMethod;
import com.ruoyi.basice_info.service.ITbAssessmentMethodService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 考核方式设置Controller
 *
 * @author hongyi
 * @date 2023-12-07
 */
@RestController
@RequestMapping("/basice_info/method")
public class TbAssessmentMethodController extends BaseController
{
    @Autowired
    private ITbAssessmentMethodService tbAssessmentMethodService;

    /**
     * 查询考核方式设置列表
     */
    @PreAuthorize("@ss.hasPermi('basice_info:method:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbAssessmentMethod tbAssessmentMethod)
    {
        startPage();
        List<TbAssessmentMethod> list = tbAssessmentMethodService.selectTbAssessmentMethodList(tbAssessmentMethod);
        return getDataTable(list);
    }

    /**
     * 导出考核方式设置列表
     */
    @PreAuthorize("@ss.hasPermi('basice_info:method:export')")
    @Log(title = "考核方式设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbAssessmentMethod tbAssessmentMethod)
    {
        List<TbAssessmentMethod> list = tbAssessmentMethodService.selectTbAssessmentMethodList(tbAssessmentMethod);
        ExcelUtil<TbAssessmentMethod> util = new ExcelUtil<TbAssessmentMethod>(TbAssessmentMethod.class);
        util.exportExcel(response, list, "考核方式设置数据");
    }

    /**
     * 获取考核方式设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('basice_info:method:query')")
    @GetMapping(value = "/{assessmentMethodId}")
    public AjaxResult getInfo(@PathVariable("assessmentMethodId") Long assessmentMethodId)
    {
        return success(tbAssessmentMethodService.selectTbAssessmentMethodByAssessmentMethodId(assessmentMethodId));
    }

    /**
     * 新增考核方式设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:method:add')")
    @Log(title = "考核方式设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbAssessmentMethod tbAssessmentMethod)
    {
        addOrEdit(tbAssessmentMethod,"add");
        return toAjax(tbAssessmentMethodService.insertTbAssessmentMethod(tbAssessmentMethod));
    }

    /**
     * 修改考核方式设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:method:edit')")
    @Log(title = "考核方式设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbAssessmentMethod tbAssessmentMethod)
    {
        addOrEdit(tbAssessmentMethod,"edit");
        return toAjax(tbAssessmentMethodService.updateTbAssessmentMethod(tbAssessmentMethod));
    }

    public void addOrEdit(TbAssessmentMethod tbAssessmentMethod, String type) {
        TbAssessmentMethod methodCode = tbAssessmentMethodService.getTbAssessmentMethodByAssessmentMethodCode(tbAssessmentMethod.getAssessmentMethodCode());
        TbAssessmentMethod methodName = tbAssessmentMethodService.getTbAssessmentMethodByAssessmentMethodName(tbAssessmentMethod.getAssessmentMethodName());
        if (methodCode!=null) {
            if(type.equals("add")) {
                throw new IllegalArgumentException("考核方式代码已存在");
            } else {
                if (!tbAssessmentMethod.getAssessmentMethodId().equals(methodCode.getAssessmentMethodId())) {
                    throw new IllegalArgumentException("考核方式代码已存在");
                }
            }
        }
        if (methodName!=null) {
            if(type.equals("add")) {

                throw new IllegalArgumentException("考核方式名称已存在");
            } else {
                if (!tbAssessmentMethod.getAssessmentMethodId().equals(methodName.getAssessmentMethodId())) {
                    throw new IllegalArgumentException("考核方式名称已存在");
                }
            }
        }
    }

    /**
     * 删除考核方式设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:method:remove')")
    @Log(title = "考核方式设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{assessmentMethodIds}")
    public AjaxResult remove(@PathVariable Long[] assessmentMethodIds)
    {
        return toAjax(tbAssessmentMethodService.deleteTbAssessmentMethodByAssessmentMethodIds(assessmentMethodIds));
    }
}
